﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using Viman.WeiXin.QQ.SDK;
using Viman.WeiXin.QQ.SDK.Entity;

namespace Viman.WeiXin.QQ.Demo
{
    /// <summary>
    /// Summary description for weixin1
    /// </summary>
    public class weixin1 : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            #region 不能修改 echostr — 微信应用提交验证
            string echostr = context.Request.QueryString["echostr"];
            if (!string.IsNullOrEmpty(echostr))
            {
                context.Response.Write(echostr);
                return;
            }
            #endregion

            #region 微信消息处理

            try
            {

                var reqmsg = RequestHelp.ReceiveMsg(context.Request);
                if (string.IsNullOrEmpty(reqmsg))
                {
                    return;
                }
                CLog.WriteLog("c:\\log", reqmsg);
                #region 接受的如果是文本消息
                if (reqmsg.Contains("<MsgType><![CDATA[text]]></MsgType>"))
                {
                    var reqMsgObj = RequestHelp.ReceiveTextMsg(reqmsg);
                    if (reqMsgObj != null)
                    {
                        //文本信息处理
                        //string response = ResponseHelp.BuildTextMsgString(reqTextMsg, "hello");
                        //context.Response.Write(response);

                        //图文消息
                        string path = GetAbsoluteUri(context);

                        ResImageMsg resMsg = new ResImageMsg();
                        resMsg.BuildMsg(reqMsgObj);
                        resMsg.Articles.Add(new Article()
                        {
                            Title = "您发的信息是:" + reqMsgObj.Content,
                            Discription = "三宝科技有限公司",
                            PicUrl = path + "images/logo.jpg",
                            Url = "http://www.samples.cn"
                        });
                        string response = ResponseHelp.BuildNewsMsgString(resMsg);
                        context.Response.Write(response);
                    }
                    else
                    {
                        throw new Exception(string.Format("文本消息处理错误,消息全文:{0}", reqmsg));
                    }
                }

                #endregion
                #region 如果接受的是位置消息
                else if (reqmsg.Contains("<MsgType><![CDATA[location]]></MsgType>"))
                {

                    var msgobj = RequestHelp.ReceiveLocationMsg(reqmsg);
                    if (msgobj != null)
                    {
                        //位置信息处理

                    }
                    else
                    {
                        throw new Exception(string.Format("位置消息处理错误,消息全文:{0}", reqmsg));
                    }
                }
                #endregion
                #region 如果接受的是图片消息
                else if (reqmsg.Contains("<MsgType><![CDATA[image]]></MsgType>"))
                {
                    var msgobj = RequestHelp.ReceiveImageMsg(reqmsg);
                    if (msgobj != null)
                    {
                        //图片信息处理

                    }
                    else
                    {
                        throw new Exception(string.Format("图片消息处理错误,消息全文:{0}", reqmsg));
                    }
                }
                #endregion
                else
                {
                    throw new Exception(string.Format("新消息类型无法处理,消息全文:{0}", reqmsg));
                }

            }
            catch (Exception ex)
            {
                CLog.WriteLog("c:\\log", ex.Message);
            }
            #endregion
        }

        private static string GetAbsoluteUri(HttpContext context)
        {
            int index = context.Request.Url.AbsoluteUri.LastIndexOf('/');
            string path = context.Request.Url.AbsoluteUri.Substring(0, index + 1);
            return path;
        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}